﻿/*******************************************************************************
 * Copyright © 2016-2020 北京网非科技有限公司 版权所有
 * Author: 北京网非科技有限公司
 * Description: 网非快速开发平台NET
 * Website：http://www.wfdot.com
*********************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CubicFWNet.Mvc
{
    public static class ExtendController
    {
        /// <summary>
        /// 获取ModelState的第一个错误消息
        /// </summary>
        /// <param name="controller"></param>
        /// <returns></returns>
        public static string GetFirstModelError(this System.Web.Mvc.Controller controller)
        {

            var errors = controller.GetErrorFromModelState();
            if (errors.Count > 0)
                foreach(var error in errors)
                {
                    if(error.Value.Count>0)
                    {
                        foreach(var e in error.Value)
                        {
                            if(!string.IsNullOrEmpty(e.ErrorMessage))
                            {
                                return e.ErrorMessage;
                            }
                        }
                    }
                }
            return string.Empty;
        }

        /// <summary>
        /// 获取ModelState中的错误信息，以字典集合的形式返回
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, ModelErrorCollection> GetErrorFromModelState(this Controller controller)
        {
            var errors = new Dictionary<string, ModelErrorCollection>();
            foreach (var key in controller.ModelState.Keys)
            {
                if (controller.ModelState[key].Errors.Count > 0)
                {
                    errors[key] = controller.ModelState[key].Errors;
                }
            }
            return errors;
        }

        /// <summary>
        /// 把部分视图转换成string
        /// </summary>
        /// <param name="controller">当前控制器</param>
        /// <param name="viewName">当前部分视图名称</param>
        /// <returns>返回字符串</returns>
        public static string RenderPartialViewToString(this Controller controller, string viewName)
        {
            return controller.RenderPartialViewToString(viewName, null);
        }

        /// <summary>
        /// 把部分视图转换成string
        /// </summary>
        /// <param name="controller">当前控制器</param>
        /// <param name="viewName">当前部分视图</param>
        /// <param name="model">Model</param>
        /// <returns>返回字符串</returns>
        public static string RenderPartialViewToString(this Controller controller, string viewName, object model)
        {
            if (string.IsNullOrEmpty(viewName))//如果部分视图名称没有
                viewName = controller.ControllerContext.RouteData.GetRequiredString("action");//action名称就是部分视图名称

            controller.ViewData.Model = model;

            using (var sw = new StringWriter())
            {
                //根据控制器上下文和部分视图名称得到ViewEngineResult
                var viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);

                //根据控制器的上下文+ViewData+ViewEngineResult中的View构建ViewContext对象实例
                var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);

                //把ViewContext对象实例写到流中
                viewResult.View.Render(viewContext, sw);

                //把流中的内容转成string
                return sw.GetStringBuilder().ToString();
            }
        }
    }
}